import java.util.ArrayList;
import java.util.List;



public class SongsSelected extends Component{

	ScrollBar verticalScrollBar;
	public int MAXSONGS = 6;
	ArrayList<String> columns;
	ArrayList<String> rows;
	ArrayList<ArrayList<String>> values;

	private float verticalScrollBarValue=0;

	public SongsSelected(int x, int y, int width, int height){
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;

		columns = new ArrayList<String>();

		columns.add("Title");columns.add("Artist");
		columns.add("Album Name");
		columns.add("Lyrics");

		updateValues();
		verticalScrollBar = new ScrollBar();
	}

	public void updateValues(){
		values = new ArrayList<ArrayList<String>>();
		for(int i=0;i<GLOBAL.songsSelected.size();i++){
			ArrayList<String> vals = new ArrayList<String>();
			vals.add(GLOBAL.songsSelected.get(i).title);
			vals.add(GLOBAL.songsSelected.get(i).artistName);
			vals.add(GLOBAL.songsSelected.get(i).album);
			String lyric;
			if(GLOBAL.songsSelected.get(i).lyrics_found){lyric = "T";}else {lyric = "F";}
			vals.add(lyric);
			values.add(vals);
		}

	}


	public void draw(){
		deactivateButtons();
		drawTable("",columns,values);
	}

	public void mousePressed(){
		for(int i=0;i<GLOBAL.removeSongsSelected.size();i++){
			if(GLOBAL.removeSongsSelected.get(i).active)
				if(GLOBAL.removeSongsSelected.get(i).mouseOver())
					GLOBAL.songAddRemove(GLOBAL.removeSongsSelected.get(i).song);
		}
	}

	public void deactivateButtons(){
		for(int i=0;i<GLOBAL.removeSongsSelected.size();i++){
			GLOBAL.removeSongsSelected.get(i).active=false;
		}
	}


	//public void drawTable(List<Season> columnNames, List<Season> rowNames/*, List<List<String>> values*/){
	public void drawTable(String graphName, List<String> columnNames, List<ArrayList<String>> values){

		Integer[] columnsWidth = {185,185,250,50};
		int columnTotalWidth = 0;
		for(int i=0;i<columnsWidth.length;i++){
			columnTotalWidth+=columnsWidth[i];
		}
		Integer[] columnsSumWidth = new Integer[columnsWidth.length+1];
		columnsSumWidth[0]=0;
		int tot =0;
		for(int i=0;i<columnsWidth.length;i++){
			tot += columnsWidth[i];
			columnsSumWidth[i+1] = tot;
		}
		int columnHeight = 30;
		int maxRows = MAXSONGS+2;
		int maxColumns = 5;
		//draw table background
		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
		GLOBAL.processing.fill(GLOBAL.colorBackground);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.noStroke();
		GLOBAL.processing.rect(x, y, width, height);


		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.fill(GLOBAL.textColor);
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);

		//draw horizontal lines of the table
		int lineLastPointX = x+22+columnTotalWidth;
		int i,j,k,l;
		//draw horizontal lines
		GLOBAL.processing.line(x+22,y+30-columnHeight,lineLastPointX,y+30-columnHeight);
		GLOBAL.processing.line(x+22,y+30,lineLastPointX,y+30);
		for(i=1;i<maxRows-1 && i<=values.size();i++){
			GLOBAL.processing.line(x+22,y+30+columnHeight*(i),lineLastPointX,y+30+columnHeight*(i));
		}
		//draw vertical lines
		for(j=0;j<columnNames.size()+1;j++){
			GLOBAL.processing.line(x+22+columnsSumWidth[j],y+columnHeight*(0),x+22+columnsSumWidth[j],y+columnHeight*(i));
		}

		//fill first row
		GLOBAL.processing.textAlign(GLOBAL.processing.CENTER);
		GLOBAL.processing.fill(GLOBAL.textBoldColor);
		GLOBAL.processing.textFont(GLOBAL.newFont, 19);

		for(k=0;k<columnNames.size();k++){
			GLOBAL.processing.text(columnNames.get(k),x +22+columnsSumWidth[k]+(-columnsSumWidth[k]+columnsSumWidth[k+1])/2 , y+30-10);
		}


		for(l=0;(l+(int)(verticalScrollBarValue+0.5))<values.size() && l<maxRows-2;l++){
			for(int column=0;column<columnNames.size();column++){
				GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
				GLOBAL.processing.fill(GLOBAL.textColor);
				String string = values.get(l+(int)(verticalScrollBarValue+0.5)).get(column);
				float len = GLOBAL.processing.textWidth(string);
				float maxDist = (columnsSumWidth[column+1]-columnsSumWidth[column]);
				if(len>maxDist){
					while(GLOBAL.processing.textWidth(string)>maxDist-10){
						string = string.substring(0,string.length()-1);
					}
				}

				if(string.equals("T")||string.equals("F")) {
					if (GLOBAL.songsArea.tick!= null && GLOBAL.songsArea.no_tick!=null) {
						GLOBAL.processing.tint(255, 150);
						if(string.equals("T"))
							GLOBAL.processing.image(GLOBAL.songsArea.tick, x +15+columnsSumWidth[column]+(-columnsSumWidth[column]+columnsSumWidth[column+1])/2, y+30*(l+1)+8);
						else
							GLOBAL.processing.image(GLOBAL.songsArea.no_tick, x +15+columnsSumWidth[column]+(-columnsSumWidth[column]+columnsSumWidth[column+1])/2, y+30*(l+1)+8);
						GLOBAL.processing.noTint();
					}
					else
						GLOBAL.processing.text(string,x +22+columnsSumWidth[column]+(-columnsSumWidth[column]+columnsSumWidth[column+1])/2, y+30*(l+1)+20);
				}else
					GLOBAL.processing.text(string,x +22+columnsSumWidth[column]+(-columnsSumWidth[column]+columnsSumWidth[column+1])/2, y+30*(l+1)+20);
			}
			GLOBAL.removeSongsSelected.get(l+(int)(verticalScrollBarValue+0.5)).x = x ;
			GLOBAL.removeSongsSelected.get(l+(int)(verticalScrollBarValue+0.5)).y = y+30*(l+1)+10;
			GLOBAL.removeSongsSelected.get(l+(int)(verticalScrollBarValue+0.5)).draw();
			GLOBAL.removeSongsSelected.get(l+(int)(verticalScrollBarValue+0.5)).active = true;
		}



		//draw vertical scrollbar
		if(values.size()>= maxRows-1){
			verticalScrollBar.width = 15;
			verticalScrollBar.x =x+22+columnTotalWidth;
			verticalScrollBar.y = y+columnHeight;
			verticalScrollBar.height = columnHeight*(i)-columnHeight-5;
			verticalScrollBar.draw();  
			verticalScrollBar.size = 1/(float)(values.size());
			verticalScrollBarValue = Main.map(verticalScrollBar.val,0,1,0,values.size()+2-maxRows);
		}
		else
			verticalScrollBarValue=0;
	}
}
